package com.weiyun.shortlink.admin.controller;

import com.weiyun.shortlink.admin.common.convention.result.Result;
import com.weiyun.shortlink.admin.common.convention.result.Results;
import com.weiyun.shortlink.admin.dto.req.ShortLinkGroupSaveReqDto;
import com.weiyun.shortlink.admin.dto.req.ShortLinkGroupSortReqDto;
import com.weiyun.shortlink.admin.dto.req.ShortLinkGroupUpdateDto;
import com.weiyun.shortlink.admin.dto.resp.ShortLinkGroupResponseDto;
import com.weiyun.shortlink.admin.service.GroupService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/***
 *   @Author:weiyun
 *
 */
@RestController
@RequiredArgsConstructor
public class GroupController {
    private final GroupService groupService;

    @PostMapping("/api/shortLink/admin/v1/group")
    public Result<Void> save(@RequestBody ShortLinkGroupSaveReqDto shortLinkGroupSaveReqDto) {
        groupService.save(shortLinkGroupSaveReqDto.getName());
        return Results.success();
    }

    @GetMapping("/api/shortLink/admin/v1/group")
    public Result<List<ShortLinkGroupResponseDto>> listGroup() {
        return Results.success(groupService.listGroup());
    }

    @PutMapping("/api/shortLink/admin/v1/group")
    public Result<Void> updateGroup(@RequestBody ShortLinkGroupUpdateDto shortLinkGroupUpdateDto) {
        groupService.updateGroup(shortLinkGroupUpdateDto);
        return Results.success();
    }

    @DeleteMapping("/api/shortLink/admin/v1/group")
    public Result<Void> deleteGroup(@RequestParam String gid) {
        groupService.deleteGroup(gid);
        return Results.success();

    }

    @PostMapping("/api/shortLink/admin/v1/group/sort")
    public Result<Void> sortGroup(@RequestBody List<ShortLinkGroupSortReqDto> shortLinkGroupSortReqDto) {
        groupService.sortGroup(shortLinkGroupSortReqDto);
        return Results.success();
    }
}
